home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / Hack / INTERNET / NOISYB_1.ZIP / NOISYB~1.JAV
Encoding:
Text File  |  1996-04-27  |  2.8 KB  |  92 lines

  1. /*  NoisyBear.java by Mark D. LaDue */
  2.  
  3. /*  February 15, 1996 */
  4.  
  5. /*  Copyright (c) 1996 Mark D. LaDue
  6.     You may study, use, modify, and distribute this example for any purpose.
  7.     This example is provided WITHOUT WARRANTY either expressed or implied.  */
  8.  
  9. /*  This Java Applet displays a stupid looking bear with a clock
  10.     superimposed on his belly.  It refuses to shut up until you quit
  11.     the browser.  */
  12.  
  13. import java.applet.AudioClip;
  14. import java.awt.*;
  15. import java.util.Date;
  16.  
  17. public class NoisyBear extends java.applet.Applet implements Runnable {
  18.     Font timeFont = new Font("TimesRoman", Font.BOLD, 24);
  19.     Font wordFont = new Font("TimesRoman", Font.PLAIN, 12);
  20.     Date rightNow;
  21.     Thread announce = null;
  22.     Image bearImage;
  23.     Image offscreenImage;
  24.     Graphics offscreenGraphics;
  25.     AudioClip annoy;
  26.     boolean threadStopped = false;
  27.  
  28.     public void init() {
  29.     bearImage = getImage(getCodeBase(), "Pictures/sunbear.jpg");
  30.     offscreenImage = createImage(this.size().width, this.size().height);
  31.     offscreenGraphics = offscreenImage.getGraphics();
  32.     annoy = getAudioClip(getCodeBase(), "Sounds/drum.au");    
  33. }
  34.  
  35.     public void start() {
  36.         if (announce == null) {
  37.         announce = new Thread(this);
  38.         announce.start();
  39.         }
  40.     }
  41.  
  42.     public void stop() {
  43.         if (announce != null) {
  44.         //if (annoy != null) annoy.stop();  //uncommenting stops the noise
  45.         announce.stop();
  46.         announce = null;
  47.         }
  48.     }
  49.  
  50.     public void run() {
  51.         if (annoy != null) annoy.loop();
  52.         while (true) {
  53.         rightNow = new Date();
  54.         repaint();
  55.         try { Thread.sleep(1000); }
  56.         catch (InterruptedException e) {}
  57.         }
  58.     }
  59.  
  60.     public void update(Graphics g) {
  61. //        g.clipRect(125, 150, 350, 50);
  62.         paint(g);
  63.     }
  64.  
  65.     public void paint(Graphics g) {
  66.         int imwidth = bearImage.getWidth(this);
  67.         int imheight = bearImage.getHeight(this);
  68.  
  69.      offscreenGraphics.drawImage(bearImage, 0, 0, imwidth, imheight, this);
  70.      offscreenGraphics.setColor(Color.white);
  71.      offscreenGraphics.fillRect(125, 150, 350, 100);
  72.      offscreenGraphics.setColor(Color.blue);
  73.      offscreenGraphics.drawRect(124, 149, 352, 102);
  74.      offscreenGraphics.setFont(timeFont);
  75.      offscreenGraphics.drawString(rightNow.toString(), 135, 200);
  76.      offscreenGraphics.setFont(wordFont);
  77.      offscreenGraphics.drawString("It's time for me to annoy you!", 135, 225);
  78.      g.drawImage(offscreenImage, 0, 0, this);
  79.     }
  80.  
  81.     public boolean mouseDown(Event evt, int x, int y) {
  82.         if (threadStopped) {
  83.             announce.resume();
  84.         }
  85.         else {
  86.             announce.suspend();
  87.         }
  88.         threadStopped = !threadStopped;
  89.         return true;
  90.     }
  91. }
  92.